from django.db import models
from base import *
from object_models import *

__all__ = ["CONTACT_ROLES", "Resource", "Entity", "Contact", "Project", "ProjectResource"]

CONTACT_ROLES = (("owner","Represents an owner of a resource."),
                 ("creator", "Created the resource."),
                 ("publisher", "Represents a publisher of a resource."),
                 ("contributor", "A general contributor to the creation of the resource."))

class Resource(MetadataObject):
    _collection_sets = []
    name = models.CharField(max_length=256)
    description = models.TextField(blank=True)
    referenceURL = models.URLField(blank=True)
    publisherDID = models.CharField(max_length=1024, blank=True)
    created = models.DateTimeField(null=True, blank=True)
    updated = models.DateTimeField(null=True, blank=True, auto_now=True)
    status = models.CharField(max_length=1024, default='valid')

    def __unicode__(self):
        return self.name

class Entity(MetadataObject):
    _collection_sets = []
    name = models.CharField(max_length=1024)
    address = models.TextField(blank=True)
    email = models.EmailField(blank=True)
    telephone = models.CharField(max_length=256, blank=True)

    def __unicode__(self):
        return self.name

class Contact(MetadataObject):
    # A contact is an entity with a role.
    _collection_sets = []
    resource = models.ForeignKey(Resource)
    entity = models.ForeignKey(Entity)
    role = models.CharField(max_length=256, 
                            choices=CONTACT_ROLES,
                            default='owner')
    _container_field = "resource_id"
    Resource._collection_sets.append('contact_set')

    def __unicode__(self):
        return self.entity.name + ': ' + self.role

class Project(Resource):
    _collection_sets = []
    element_name = 'aProject'

class ProjectResource(MetadataObject):
    _collection_sets = []
    project = models.ForeignKey(Project, related_name='project_resource')
    experiment = models.ForeignKey(Resource)
    _container_field = "project_id"
    Project._collection_sets.append('project_resource')
